<template>
  <div class="dictionaryCost h100">
    <div class="PageContent">
      <el-row style="height: 100%;">
        <el-col :span="4" class="left-tree">
          <el-tree :data="treeCostData" ref="dictCostTree"
                   :props="defaultProps"
                   node-key="id"
                   default-expand-all
                   check-strictly
                   highlight-current
                   @node-click="handleCheck"
          ></el-tree>
        </el-col>
        <el-col :span="20" class="right-tree">
          <el-row style="text-align: left;line-height: 5em;">
            <el-button type="primary" plain v-if="btnRight.btnDictionaryCostAddRule" @click="addRule()">设置规则</el-button>
            <el-button type="primary" v-if="btnRight.btnDictionaryCostAddPurpose" @click="modifyRule()">追加用途规则
            </el-button>
            <el-button type="danger" v-if="btnRight.btnDictionaryCostDeleteRule" @click="deleteRule()">删除用途规则
            </el-button>
          </el-row>
          <el-table
            ref="singleCostTable" :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
            :data="dictionaryTableData"
            stripe
            border
            @row-click="handleRowClick"
            @select="select" @select-all="selectAll"
            style="width: 100%;height: calc(100% - 80px);overflow-y: auto;">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              property="dataDictionaryText"
              label="类型"
              align='center'
              width="150">
            </el-table-column>
            <el-table-column
              property="serviceItemName"
              label="费用名称"
              align='center'
              width="150">
            </el-table-column>
            <el-table-column
              property="purpose"
              label="用途"
              align='center'
              width="150">
            </el-table-column>
            <el-table-column
              property="ruleCode"
              align='center'
              label="规则编码"
              width="150">
            </el-table-column>
            <el-table-column
              property="ruleType"
              align='center'
              label="规则类型"
              width="150">
            </el-table-column>
            <el-table-column
              property="ruleDescribe"
              align='center'
              label="规则描述"
              min-width="500">
            </el-table-column>
          </el-table>

        </el-col>
      </el-row>
    </div>
    <el-dialog title="字典费用关联及计算规则设置" :visible.sync="dialogCostVisibleType" width="50%" v-dialogDrag border
               @close="closeDialog()">
      <el-form :model="saveDictionaryCost" :rules="rules" ref="costDialog" :inline="true" label-width="200px"
               :disabled="saveFlag">
        <el-row :gutter="5">
          <el-form-item label="字典项:">
            <span>{{saveDictionaryCost.dataDictionaryText}}</span>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="关联费用:" prop="serviceItemName">
            <el-input v-model="saveDictionaryCost.serviceItemName" disabled="true"></el-input>
          </el-form-item>
          <el-button type="primary" plain v-if="btnRight.btnDictionaryCostOpenCharge" @click="add()">选择</el-button>
        </el-row>

        <el-row :gutter="5">
          <el-form-item label="价格:">
            <el-input v-model="saveDictionaryCost.serviceItemPrice" :maxlength="50" @change="changPrice()" :readonly="asFixPrice"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="用途:" prop="purpose">
            <el-select v-model="saveDictionaryCost.purpose" style="width: 185px;" placeholder="请选择" :maxlength="50">
              <el-option
                v-for="item in purpose"
                :label="item.purposeText"
                :value="item.purposeCode"/>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="计算规则:">
            <el-select v-model="saveDictionaryCost.ruleCode" onkeydown="return (event.keyCode!=8)" style="width: 185px;" placeholder="请选择"
                       @change="ruleChange" :maxlength="50">
              <el-option
                v-for="item in calculateRules"
                :label="item.ruleType"
                :value="item.ruleCode">
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="规则编码:">
            <el-input v-model="saveDictionaryCost.ruleCode" :maxlength="50" :readonly="true"></el-input>
          </el-form-item>
        </el-row>

        <el-row :gutter="5">
          <el-form-item label="规则类型:">
            <el-input v-model="saveDictionaryCost.ruleType" :maxlength="20" :readonly="true"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="规则描述:">
            <el-input type="textarea" v-model="ruleDescribeRemark" style="width: 495px;" :readonly="true"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="规则参数:"></el-form-item>
        </el-row>
        <el-row style="padding-left: 100px;" v-for="(item,index) in ruleParams">
          <el-form-item :label="item.label" style="width:200px;"></el-form-item>
          <el-form-item v-if="item.name=='itemPrice'">
            <span>{{item.value}}</span>
          </el-form-item>
          <el-form-item v-if="item.name!='itemPrice'">
            <el-input v-model="item.value" @input="inputChange($event)"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="规则说明:">
            <el-input type="textarea" v-model="ruleDescribeStr"
                      style="width: 495px;"></el-input>
          </el-form-item>
        </el-row>
      <!--  <el-row :gutter="5">
          <el-form-item label="原规则说明:">
            <el-input type="textarea" v-model="saveDictionaryCost.ruleDescribe" style="width: 495px;"
                      :readonly="true"></el-input>
          </el-form-item>
        </el-row>-->
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <el-button type="primary" v-if="btnRight.btnDictionaryCostSave" @click="submit()">保存</el-button>
        <el-button type="primary" plain v-if="btnRight.btnDictionaryCostCancel" @click="cancelForm()">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="费用列表" :visible.sync="DialogChargeService" v-dialogDrag width="700" border>
      <el-row style="line-height: 40px;margin-bottom: 10px;">
        <label>根据项目名称或单价检索：
          <el-input v-model="search" style="width: 300px;"></el-input>
        </label>
      </el-row>
      <el-row style=" height:500px;overflow-y: scroll">
        <el-table
          ref="chartsTable"
          :data="tables"
          :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
          stripe
          tooltip-effect="dark"
          style="width: 100%"
          @select="selectdialog" @select-all="selectAlldialog"
          @selection-change="handleSingleChange">
          <el-table-column
            type="selection"
            width="100">
          </el-table-column>
          <el-table-column align='center' prop="itemName" label="项目名称" min-width="200">
          </el-table-column>
          <el-table-column
            prop="itemPrice"
            align='right'
            label="单价"
            width="100">
          </el-table-column>
          <el-table-column
            prop="itemNumber"
            align='right'
            label="数量"
            width="100">
          </el-table-column>
          <el-table-column
            prop="itemUnit"
            align='center'
            label="单位"
            width="100">
          </el-table-column>
          <el-table-column
            prop="asGoods"
            align='center'
            label="是否物品"
            width="100">
            <template slot-scope="{row,$index}">
              <span v-if="row.asGoods==0">服务</span>
              <span v-else-if="row.asGoods==1">物品</span>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <el-button type="primary" v-if="btnRight.btnDictionaryCostSelectCharge" @click="Save()" >保 存</el-button>
        <el-button type="primary" plain v-if="btnRight.btnDictionaryCostCancelCharge" @click="DialogChargeService = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import * as API from '@/api/systemApplication'
    import {treeDictionaryformate} from "../../utils/treeDictionaryformate";

    export default {
        name: "dictionaryCost",
        data() {
            return {
                rules: {//saveDictionaryCost.serviceItemName
                    serviceItemName: [{required: true, message: '请选择', trigger: 'blur'}],
                    purpose: [{required: true, message: '请输入', trigger: 'blur'}],
                },
                treeCostData: [{
                    label: '字典类型',
                    children: []
                }],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                dictionaryTableData: [{
                    "id": 0,
                    "dataDictionaryText": "",//显文本
                    "serviceItemName": "",//低档车型费
                    "purpose": "",//用途
                    "ruleCode": "",//规则编码
                    "ruleType": "",//规则类型
                    "ruleDescribe": "", //规则描述
                    "serviceTypeId": 0,
                    "serviceTypeName": "string",
                    "itemCode": "string",
                    "itemName": "string",
                    "itemPrice": 0,
                    "itemNumber": 0,
                    "itemUnit": "string",
                    "asGoods": 0,
                    "asPackage": 0,
                    "asFixPrice": 0,
                    "disableState": 0,
                    "itemDescribe": "string"
                }],
                //计算规则
                calculateRules: [],
                //用途
                purpose: [],
                //当前行
                currentRow: null,
                tableSelection: [],
                selectedDictionaryId: 0,//选择字典id
                selectedId: 0,//选择字典规明细id
                selectedVersion: 0,//选中版本
                onpresscTime:0,  //  阻止短时间内连续点击
                saveFlag: false, //保存标志
                asFixPrice:true,//是否固定价格，true只读固定价格，false非只读，议价
                dialogCostVisibleType: false,  //项目分类维护对话框
                saveDictionaryCost: {       //保存字典信息
                    "id": 0,
                    "dataDictionaryId": 0,
                    "dataDictionaryText": "",//显文本
                    "serviceItemName": "",//低档车型费
                    "serviceItemId": 0,//费用外键（必填）
                    "purpose": "",//用途
                    "ruleCode": "",//规则编码
                    "ruleType": "",//规则类型
                    "ruleDescribe": "", //规则描述
                    "serviceItemPrice": ""//价格
                },
                //规则参数
                ruleParams:[],
                search: "",    //模糊查询
                sigleSelection: [],//选中费用
                chargeServiceList: [],//费用列表goodsServiceList
                DialogChargeService: false,//费用选择对话框,
                dictCode: "",//选中字典编码
                ruleDescribeRemark: "",// 规则模板
                ruleDescribeStrRemark: "",// 规则模板
                btnRight: {
                    btnDictionaryCostAddRule: true, //设置规则
                    btnDictionaryCostAddPurpose: true,//追加用途规则
                    btnDictionaryCostDeleteRule: true,//删除用途规则
                    btnDictionaryCostSave: true,//规则保存
                    btnDictionaryCostCancel: true,//规则取消
                    btnDictionaryCostOpenCharge: true,//关联费用选择
                    btnDictionaryCostSelectCharge: true,//保存关联费用
                    btnDictionaryCostCancelCharge: true,//关闭关联窗口
                }
            }
        },
        mounted() {
            //载入选中字典类型
            this.loadDictionaryCostList()
            //载入按钮
            this.btnRight = this.$route.meta.buttons;//mounted(){}
            //遍历属性赋值将true与false字符串转换成布尔型的值
            for (let key of Object.keys(this.btnRight)) {
              this.btnRight[key]=(this.btnRight[key]|| this.btnRight[key]=="true")?true:false;
            }
        },
        computed: {
            // 说明
            ruleDescribeStr() {
                let ruleDescStr = this.ruleDescribeRemark;
                let itemData = this.ruleParams;
                let itemValue = 0;
                let labelName = "";
                if (ruleDescStr && itemData.length && itemData.length > 0) {
                    for (let i = 0; i < itemData.length; i++) {
                        labelName = itemData[i].name;
                        itemValue = itemData[i].value;
                        labelName = "【" + labelName + "】";
                        if (ruleDescStr.indexOf(labelName) != -1) {
                            ruleDescStr = ruleDescStr.replace(RegExp(labelName, "g"), itemValue);
                        }
                    }
                }
                this.ruleDescribeStrRemark = ruleDescStr;
                return ruleDescStr;
            },
            // 模糊搜索
            tables() {
                const search = this.search
                if (search) {
                    // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
                    return this.chargeServiceList.filter(data => {
                        // some() 方法用于检测数组中的元素是否满足指定条件;
                        {
                            return Object.keys(data).some(key => {
                                // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
                                // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
                                if (key == "itemName" || key == "itemPrice") {
                                    return String(data[key]).indexOf(search) > -1
                                }
                            })
                        }

                    })
                }
                return this.chargeServiceList
            }
        },
        methods: {
            // 说明
            ruleDescribe() {
                let ruleDescStr = this.ruleDescribeRemark;
                let itemData = this.ruleParams;
                let itemValue = 0;
                let labelName = "";
                if (ruleDescStr && itemData.length && itemData.length > 0) {
                    for (let i = 0; i < itemData.length; i++) {
                        labelName = itemData[i].name;
                        itemValue = itemData[i].value;
                        labelName = "【" + labelName + "】";
                        if (ruleDescStr.indexOf(labelName) != -1) {
                            ruleDescStr = ruleDescStr.replace(RegExp(labelName, "g"), itemValue);
                        }
                    }
                }
                return ruleDescStr;
            },
            // 价格调整
            changPrice() {
               //调用价格设置参数
               this.setItemPrice();
            },//设置参数价格
             setItemPrice(  ){
               let itemData = this.ruleParams;
               if (itemData.length && itemData.length > 0) {
                 //价格变化时，改变参数中价格的值
                 for (let i = 0; i < itemData.length; i++) {
                   if (itemData[i].name == 'itemPrice') {

                     itemData[i].value = this.saveDictionaryCost.serviceItemPrice;
                   }
                 }
               }
            },
            //字典基本信息
            loadDictionaryCostList() {
                let postData = {}
                API['loadServiceDictionaryList'](postData).then(response => {
                    const {data} = response;
                    console.log(data,55555);
                    if (!data) {
                        this.$message('数据读取失败')
                        return
                    }
                    this.treeCostData = treeDictionaryformate(data)
                    // console.info(this.treeCostData);
                }).catch(error => {
                    this.$message(error)
                })
            },
            change(val) {
                val = val.replace(/(^\s*)|(\s*$)/g, "")
                if (!val) {
                    this.saveDictionaryCost.dataCode = "";
                    return
                }
                let reg = /[^\d]/g
                // 只能是数字和小数点，不能是其他输入
                val = val.replace(reg, "")
                this.saveDictionaryCost.dataCode = val;
            },//用途改变时触发
            ruleChange(selVal) {
                //规则编码
                let ruleCode = selVal;
                //规则类型
                let ruleType = "";
                //规则描述
                let ruleDescribe = "";
                this.ruleParams = [];
                if (this.calculateRules && this.calculateRules.length > 0) {
                    let data = this.calculateRules;
                    let item = {};
                    for (let i = 0; i < data.length; i++) {
                        item = data[i];
                        if (selVal == item.ruleCode) {
                            ruleType = item.ruleType;
                            ruleDescribe = item.ruleDescribe;
                            break;
                        }
                    }
                }
                //规则参数
                let ruleParamsStr = ruleDescribe;
                while (ruleParamsStr.indexOf('【') != -1 && ruleParamsStr.indexOf('】') != -1) {
                    let start = ruleParamsStr.indexOf('【');
                    let end = ruleParamsStr.indexOf('】');
                    let paramName = ruleParamsStr.substring(start + 1, end);
                    ruleParamsStr = ruleParamsStr.substring(end + 1);
                    let obj = {
                        name: paramName,
                        label: paramName + "：",
                        value: 0
                    }

                    // 判断价格
                    if (paramName == 'itemPrice') {
                        obj.value = this.saveDictionaryCost.serviceItemPrice;
                    }
                    //查询是否参数有重复
                    let flag_exist = false;
                    if (this.ruleParams && this.ruleParams.length > 0) {
                        for (let j = 0; j < this.ruleParams.length; j++) {
                            if (this.ruleParams[j].name == paramName) {
                                flag_exist = true;
                                break;
                            }
                        }
                    }
                    //不重复，加入参数数组
                    if (!flag_exist) {
                        this.ruleParams.push(obj);
                    }

                }
                //规则赋值
                this.saveDictionaryCost.ruleCode = ruleCode;
                // 规则类型
                this.saveDictionaryCost.ruleType = ruleType;
                // 规则模板描述
                this.ruleDescribeRemark = ruleDescribe;
                //  规则描述
                this.ruleDescribeStrRemark = this.ruleDescribe();
            },//设置参数
            setRuleItem(obj){
              let this_rule=this;
            if(obj){
              for (let key in obj) {
                //判断参数据是否重复
                let oldObj=this_rule.getExists(this_rule.ruleParams,key);
                let value = obj[key];
                if( oldObj ==-1){
                  this_rule.ruleParams.push({
                    name: key,
                    label: key + "：",
                    value: value
                  });
                }else{
                  //数据已存在，进行重新赋值
                  this_rule.ruleParams[oldObj]= {
                    name: key,
                    label: key + "：",
                    value: value
                  };
                }
              }
            }
          },//判断参数val是否在arry数据中存在
             isExists(arry, val) {
                let flag_exists = false;
                if (arry && arry.length > 0) {
                    for (let k = 0; k < arry.length; k++) {
                        if (arry[k].name == val) {
                            flag_exists = true;
                            return flag_exists;
                        }
                    }
                }
                return flag_exists;
            }, //查换数组并返回存在的明细下标
            getExists(arry, val) {
            let oldObj=-1;
            if (arry && arry.length > 0) {
              for (let k = 0; k < arry.length; k++) {
                if (arry[k].name == val) {
                  oldObj =k;
                  return oldObj;
                }
              }
            }
            return oldObj;
          },
            //设置规则
            addRule() {
                let node = this.$refs.dictCostTree.getCheckedNodes()
                let data = this.$refs.singleCostTable.selection
                let id, selectedDictionaryId
                if (node && node.length) {
                    if (data && data.length) {
                        selectedDictionaryId = data[0].dataDictionaryId
                        id = data[0].id //选择id
                        let postData = {id: id, dataDictionaryId: selectedDictionaryId};
                        let this_add = this;
                        //载入字典明细
                        API['loadServiceDictionaryDataWithId'](postData).then(response => {
                            let data = response;
                            if (!data) {
                                this.$message(data.message)
                                return
                            } else {
                                this_add.saveDictionaryCost = data.data.serviceDictionaryItem;
                                //计算规则
                                let calculateRules = data.data.calculateRules;
                                //用途
                                let purpose = data.data.purpose;

                                //保存规则，及用途
                                this_add.calculateRules = calculateRules;
                                this_add.purpose = purpose;
                                //规则变化
                                this_add.ruleChange(this_add.saveDictionaryCost.ruleCode);
                                if (this_add.saveDictionaryCost && this_add.saveDictionaryCost.ruleParams) {
                                    //this_add.ruleParams = [];
                                   //规则参数解析
                                    let paramObj=JSON.parse(this_add.saveDictionaryCost.ruleParams);
                                    if(paramObj){
                                      this_add.setRuleItem(paramObj);
                                    }
                                }
                                this_add.ruleDescribeStrRemark = this_add.ruleDescribe();
                                this_add.selectedDictionaryId = this_add.saveDictionaryCost.dataDictionaryId;
                                this_add.dialogCostVisibleType = true;
                            }
                        }).catch(error => {
                            this.$message(error)
                        })
                    } else {
                        this.$message("未选中规则")
                    }
                } else {
                    this.$message("未选中系统字典")
                }

            }, add() {
                API['loadGoodsServiceList']().then(response => {
                    const {data} = response;
                    if (!data) {
                        this.$message('数据读取失败')
                        return
                    }
                    this.chargeServiceList = data
                    if (this.chargeServiceList.length) {
                        this.DialogChargeService = true
                    }
                }).catch(error => {
                    this.$message(error)
                })

            }, handleSingleChange(val) {
                this.sigleSelection = val;
            },
            //服务物品保存
            Save() {
                let selectData = this.sigleSelection;
                this.saveDictionaryCost.serviceItemName = selectData[0].itemName;
                this.saveDictionaryCost.serviceItemPrice = selectData[0].itemPrice;
                this.saveDictionaryCost.serviceItemId = selectData[0].id;
              if(selectData[0] && selectData[0].asFixPrice==0){
                //可以修改价格，只读为false
                this.asFixPrice=false;
                }
                //改变参数中的价格,调用价格设置参数
                this.setItemPrice();

                this.DialogChargeService = false
            },
            //用途追加
            modifyRule() {
                let node = this.$refs.dictCostTree.getCheckedNodes()
                let data = this.$refs.singleCostTable.selection
                let id, selectedDictionaryId
                if (node && node.length) {
                    if (data && data.length) {
                        selectedDictionaryId = data[0].dataDictionaryId
                        id = data[0].id //选择id

                        let postData = {id: id, dataDictionaryId: selectedDictionaryId};
                        let this_modify = this;
                        //载入字典明细
                        API['loadServiceDictionaryDataWithId'](postData).then(response => {
                            let data = response;
                            if (!data) {
                                this_modify.$message(data.message)
                                return
                            } else {
                                let serviceDictionaryItem=(data.data.serviceDictionaryItem)?data.data.serviceDictionaryItem:null;
                                this_modify.saveDictionaryCost = serviceDictionaryItem;
                               //规则模板
                               this_modify.saveDictionaryCost.ruleDescribeRemark= serviceDictionaryItem.calculateRuleDescribe;
                               //规则类型
                               this_modify.saveDictionaryCost.ruleType= serviceDictionaryItem.ruleType;
                                //规则
                                this_modify.ruleChange(this_modify.saveDictionaryCost.ruleCode);
                                let objParam = JSON.parse(serviceDictionaryItem.ruleParams);
                                //追加新用途，id->0version->0-purpose-""
                                this_modify.saveDictionaryCost.id = 0;
                                this_modify.saveDictionaryCost.version = 0;
                                this_modify.saveDictionaryCost.purpose = "";
                                //计算规则
                                let calculateRules = data.data.calculateRules;
                                //用途
                                let purpose = data.data.purpose;
                                this_modify.calculateRules = calculateRules;
                                this_modify.purpose = purpose;
                                //设置参数
                                if(objParam){
                                  this_modify.setRuleItem(objParam);
                                }
                                this_modify.ruleDescribeStrRemark = this_modify.ruleDescribe;
                                this_modify.selectedDictionaryId = this_modify.saveDictionaryCost.dataDictionaryId;
                                this_modify.dialogCostVisibleType = true;
                            }
                        }).catch(error => {
                            this.$message(error)
                        })
                    } else {
                        this.$message("未选中规则")
                    }
                } else {
                    this.$message("未选中系统字典")
                }
            },
            closeDialog() {
                this.saveDictionaryCost = {};
                this.dialogCostVisibleType = false;
            },//根据明细id删除明细数据
            deleteRule() {
                let data = this.$refs.singleCostTable.selection

                if (data && data.length) {
                    if (data[0].id && data[0].version) {
                      this.$confirm('删除信息, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                      }).then(() => {
                            let postData = [{
                                "id": data[0].id,
                                "version": data[0].version
                            }];
                            let this_delete = this;
                            //载入字典明细
                            API['deleteServiceDictionaryDataWithId'](postData).then(response => {
                                let data = response;
                                if (!data) {
                                    this.$message(data.message)
                                    return
                                } else {
                                    this.$message(data.message)
                                }
                                //加载data
                                //加载字典明细项
                                this_delete.loadServiceDictionaryDataListWithCode(this_delete.dictCode);
                            }).catch(error => {
                                this.$message(error)
                            })
                      }).catch(() => {
                        //几点取消的提示
                      });
                    } else {
                        this.$message("获取不到规则")
                    }

                } else {
                    this.$message("未选中规则")
                }
            },
            //根据id加载物品，服务类型详情
            loadServiceDictionaryDataListWithCode(dictCode) {
                let postData = {
                    "dictCode": dictCode
                }
                let this_load = this;
                API['loadServiceDictionaryDataListWithCode'](postData).then(response => {
                    const {data} = response;
                    if (!data) {
                        this_load.$message('数据读取失败')
                        return
                    }
                    let loadData = treeDictionaryformate(data);
                    //获得第一个返回值的children
                    this_load.dictionaryTableData = loadData[0].children;
                }).catch(error => {
                    this_load.$message(error)
                })
            },
            //tree单选
            handleCheck(data, checked, node) {
                this.$refs.dictCostTree.setCheckedNodes([data]);
                node = this.$refs.dictCostTree.getCheckedNodes();
                // console.log(node[0].dictCode);
                this.dictCode = data.dictCode;
                let this_local = this;
                //描述
                this_local.saveDictionaryCost.dataDescribe = data.dictText;
                let dictCode=data.dictCode;
                if(dictCode){
                  //类型编码，记录字典编码code
                  this_local.saveDictionaryCost.dictCode = data.dictCode;
                  //加载字典明细项
                  this_local.loadServiceDictionaryDataListWithCode(data.dictCode);
                }

            },
            //点击行触发，选中或不选中复选框
            handleRowClick(row, column, event) {
                if (this.tableSelection.length > 0) {
                    this.$refs.singleCostTable.toggleRowSelection(this.tableSelection[0], false);
                }
                this.$refs.singleCostTable.toggleRowSelection(row);
                this.tableSelection = [row]
            },
            select(selection, row) {
                // 选择项大于1时
                if (selection.length > 1) {
                    let del_row = selection.shift();
                    // console.log('把数组的第一个元素从其中删除后', selection);
                    this.$refs.singleCostTable.toggleRowSelection(del_row, false); // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
                }
                this.tableSelection = selection;
                //选择收费规则id
                this.selectedId = row.id;
                this.selectedDictionaryId = row.dataDictionaryId;
                this.selectedVersion = row.version;
                // console.log('当用户手动勾选数据行的 Checkbox 时触发的事件', selection, row)
            },
            selectAll(selection) {
                // console.log('当用户手动勾选全选 Checkbox 时触发的事件', selection)
                if (selection.length > 1) {
                    selection.length = 1;
                }
                this.tableSelection = selection
            },
            /*费用单选*/
            selectdialog(selection, row) {
                // 选择项大于1时
                if (selection.length > 1) {
                    let del_row = selection.shift();
                    // console.log('把数组的第一个元素从其中删除后', selection);
                    this.$refs.chartsTable.toggleRowSelection(del_row, false); // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
                }
                this.sigleSelection = selection;
            },
            selectAlldialog(selection) {
                if (selection.length > 1) {
                    selection.length = 1;
                }
                this.sigleSelection = selection
            },
            //字典费用规则保存
            submit() {
              if ((Date.now() - this.onpresscTime) > 2000) {
                this.onpresscTime = Date.now()
                console.log(Date.now())
              }else{
                return
              }
                let paramData = this.ruleParams;
                //参数数组
                let paramsStr = "";
                let paramsObj = {};
                if (paramData && paramData.length > 0) {
                    for (let i = 0; i < paramData.length; i++) {
                        let item = paramData[i];
                        paramsObj['' + item.name + ''] = item.value;
                    }
                }
                paramsStr = JSON.stringify(paramsObj);
                let postData = {
                    "dataDictionaryId": this.selectedDictionaryId,
                    "id": this.saveDictionaryCost.id,
                    "purpose": this.saveDictionaryCost.purpose,
                    "ruleCode": this.saveDictionaryCost.ruleCode,
                    "ruleDescribe": this.ruleDescribeStrRemark,
                    "ruleParams": paramsStr,
                    "ruleType": this.saveDictionaryCost.ruleType,
                    "serviceItemId": this.saveDictionaryCost.serviceItemId,
                    "version": this.saveDictionaryCost.version
                };
                let this_submit = this;
              this.saveFlag = true;
                API['saveServiceDictionaryData'](postData).then(response => {
                    if (response) {
                        let data = response;
                        if (!data) {
                          this.saveFlag = false;
                            this_submit.message('数据读取失败')
                            return
                        }
                        this_submit.dialogCostVisibleType = false;
                        this_submit.saveDictionaryCost = {};
                        //加载字典明细项
                        this_submit.loadServiceDictionaryDataListWithCode(this_submit.dictCode);
                      this.saveFlag = false;
                    } else {
                        this_submit.message('数据保存失败')
                      this.saveFlag = false;
                    }

                }).catch(error => {
                    this_submit.message(error)
                  this.saveFlag = false;
                })
              this.saveFlag = false;
            },//取消
            cancelForm() {
                this.saveDictionaryCost = {};
                //清除规则参数
                this.ruleParams=[];
                //清除规则描述
                this.saveDictionaryCost.ruleDescribe="";
                this.dialogCostVisibleType = false
            },//input不能修改，需要强制视图更新
            inputChange(e){
                this.$forceUpdate()
          }
        },
        watch: {
            "dialogCostVisibleType"() {
                if (this.dialogCostVisibleType == false) {
                    this.$nextTick(() => {
                        this.$refs.costDialog.resetFields();
                    })
                }
            }
        }
    }
</script>

<style scoped>

</style>
